package utils

import (
	"time"
)

const TimestampLayout = "2006-01-02 15:04:05"
const TimestampLayoutMillisecond = "2006-01-02T15:04:05.000-0700"
const TimestampLayoutYMD = "20060102"
const TimestampLayoutYMD2 = "2006-01-02"
const TimestampLayoutYM = "200601"
const TimestampLayout1 = "2006-01-02T15-04-05"

var (
	MinuteSeconds  int64 = 60
	DaySeconds     int64 = 86400
	HourSeconds    int64 = 60 * 60
	WeekSeconds    int64 = 7 * 86400
	YearSeconds    int64 = 365 * 86400
	TenYearSeconds int64 = 10 * YearSeconds
	DayHour        int   = 24
)

// GetNowTime 获取当前时间戳
func GetNow() time.Time {
	return time.Now()
}

// GetNowTime 获取当前时间戳
func GetNowByLayout(layout string) string {
	return time.Now().Format(layout)
}

// GetNowTime 获取当前时间戳
func GetNowTime() int64 {
	return time.Now().Unix()
}

func GetNowMinute() int64 {
	return GetNowTime() / 60
}

func GetTodayMinute() int64 {
	now := GetNow()
	return int64(now.Hour())*60 + int64(now.Minute())
}

// GetNowTime 获取当前时间戳
func FormatTimeStamp(t int64) string {
	return UnixToTime(t).Format(TimestampLayout)
}

func GetNowWeekDay() int64 {
	weekDay := time.Now().Weekday()
	if weekDay == 0 {
		weekDay = 7
	}
	return int64(weekDay)
}

// TimeStrToUnix 时间字符串转成时间戳
func TimeStrToUnix(timeStr string) int64 {
	if timeStr == "" {
		return 0
	}
	t1, _ := time.ParseInLocation(TimestampLayout, timeStr, time.Local)
	return t1.Unix()
}

// TimeStrToUnix 时间字符串转成时间戳
func TimeStrToUnixByLayout(timeStr, layout string) int64 {
	if timeStr == "" {
		return 0
	}
	t1, _ := time.ParseInLocation(layout, timeStr, time.Local)
	return t1.Unix()
}

func IsEqualDay(t1, t2 time.Time) bool {
	if t1.Year() == t2.Year() &&
		t1.Month() == t2.Month() &&
		t1.Day() == t2.Day() {
		return true
	}
	return false
}

func IsEqualDayUnix(t1Unix, t2Unix int64) bool {
	t1 := UnixToTime(t1Unix)
	t2 := UnixToTime(t2Unix)

	if t1.Year() == t2.Year() &&
		t1.Month() == t2.Month() &&
		t1.Day() == t2.Day() {
		return true
	}
	return false
}

func NowTimeStr() string {
	now := time.Now()
	timeString := now.Format(TimestampLayout)
	return timeString
}

func TodayStr() string {
	now := time.Now()
	timeString := now.Format("2006-01-02")
	return timeString
}

// IsToday 是否为今天
func IsToday(timestamp int64) bool {
	tm := time.Unix(timestamp, 0) // 转换成time.Time对象

	year, month, day := tm.Date() // 获取时间戳的年月日

	todayYear, todayMonth, todayDay := time.Now().Date() // 获取当前时间的年月日

	return year == todayYear && month == todayMonth && day == todayDay // 如果年、月、日相等，那么该时间戳就是今天的
}

// UnixToTime 时间戳转换为时间
func UnixToTime(timestamp int64) time.Time {
	// 将时间戳转换为time.Time类型
	return time.Unix(timestamp, 0)
}

// ConvertToTime 将输入的年月日时分秒转换为对应的时间Time
func ConvertToTime(year int, month int, day int, hour int, minute int, second int) time.Time {
	// 时间格式化，根据在本地时间构建新的时间对象
	loc, _ := time.LoadLocation("Local")
	return time.Date(year, time.Month(month), day, hour, minute, second, 0, loc)
}

// 获取当天零点时间戳
func GetTimeZero(t1 int64) int64 {
	t := time.Unix(t1, 0)                                         // 从时间戳创建时间对象
	year, month, day := t.Date()                                  // 获取日期
	location := t.Location()                                      // 获取时区信息
	midnight := time.Date(year, month, day, 0, 0, 0, 0, location) // 创建当天0点的时间对象
	return midnight.Unix()                                        // 返回零点的时间戳
}

// GetYMStrByTimeStamp
func GetYMStrByTimeStamp(timeStamp int64) string {
	return UnixToTime(timeStamp).Format(TimestampLayoutYM)
}

// GetYearMonDayStrByTimeStamp
func GetYMDStrByTimeStamp(timeStamp int64) string {
	return UnixToTime(timeStamp).Format(TimestampLayoutYMD)
}

// GetYMD2StrByTimeStamp
func GetYMD2StrByTimeStamp(timeStamp int64) string {
	return UnixToTime(timeStamp).Format(TimestampLayoutYMD2)
}

// GetTodayTimeScop
func GetZeroTimeScop(timeStamp int64) (int64, int64) {
	beginTime := GetTimeZero(timeStamp)
	return beginTime, beginTime + DaySeconds
}

// GetNextHourUnix
func GetNextHourUnix(h int) int64 {
	now := GetNow()
	t := ConvertToTime(now.Year(), int(now.Month()), now.Day(), h, 0, 0)

	nowHour := now.Hour()
	if nowHour > h {
		return t.Unix() + DaySeconds
	}
	return t.Unix()
}
